package com.fyzk.activity.lottery.basketball;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tyjc.lottery.R;
import com.fyzk.model.JCLQMatchData;
import com.fyzk.model.JCLQMatchData.SPInfo;

import java.util.ArrayList;
import java.util.List;

public class JL_HG_Pop extends PopupWindow implements View.OnClickListener {

    private JCLQMatchData data;

    private CheckBox hg_sf_home, hg_sf_away;// 非让分

    private CheckBox hg_rsf_home, hg_rsf_away;// 让分

    private CheckBox hg_dxf_big, hg_dxf_small;// 大小分

    private CheckBox hg_sfc_ke_15, hg_sfc_ke_610, hg_sfc_ke_1115,
            hg_sfc_ke_1620, hg_sfc_ke_2125, hg_sfc_ke_26;// 胜分差 客胜
    private CheckBox hg_sfc_zhu_15, hg_sfc_zhu_610, hg_sfc_zhu_1115,
            hg_sfc_zhu_1620, hg_sfc_zhu_2125, hg_sfc_zhu_26;// 胜分差 主胜

    private LinearLayout jl_hg_sfc_layout;
    private LinearLayout ll_hg_dxf;
    private TextView jl_hg_sfc_stop;
    private TextView hg_dxf_stop;

    private String[] sfc_arrs = null;

    private TextView away_tv;
    private TextView home_tv;

    protected LinearLayout center_ll;

    private TextView bottom_sure_tv;
    private TextView bottom_cancel_tv;

    private Context context;

    public JL_HG_Pop(Context context, int width, int height, JCLQMatchData data) {
        super(width, height);
        this.data = data;
        this.context = context;
        View view = View.inflate(context, R.layout.pop_basketball_hg, null);
        setContentView(view);
        initView(view);
        sfc_arrs = new String[]{data.SFC_11, data.SFC_12, data.SFC_13, data.SFC_14, data.SFC_15, data.SFC_16, data.SFC_01, data.SFC_02, data
                .SFC_03, data.SFC_04, data.SFC_05, data.SFC_06};
        setOutsideTouchable(true);
    }


    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.betcheckdialog_cancel:
                onClickCancel();
                break;
            case R.id.betcheckdialog_sure:
                onClickSure();
                break;
            default:
                break;
        }
    }

    List<JCLQMatchData.SPInfo> tempSpInfos = new ArrayList<>();

    /**
     * 控件初始化
     */
    private void initView(View view) {
        away_tv = view.findViewById(R.id.betcheckdialog_away_tv);
        away_tv.setText(setAway());
        home_tv = view.findViewById(R.id.betcheckdialog_home_tv);
        home_tv.setText(setHome());
        center_ll = view.findViewById(R.id.betcheckdialog_center_ll);
        ll_hg_dxf = view.findViewById(R.id.ll_hg_dxf_jl);
        bottom_cancel_tv = view.findViewById(R.id.betcheckdialog_cancel);
        bottom_cancel_tv.setOnClickListener(this);
        bottom_sure_tv = view.findViewById(R.id.betcheckdialog_sure);
        bottom_sure_tv.setOnClickListener(this);
        jl_hg_sfc_layout = view.findViewById(R.id.jl_hg_sfc_layout);
        jl_hg_sfc_stop = view.findViewById(R.id.jl_hg_sfc_stop);
        hg_sf_home = view.findViewById(R.id.jl_hg_sf_home);
        hg_sf_away = view.findViewById(R.id.jl_hg_sf_away);
        hg_rsf_home = view.findViewById(R.id.jl_hg_rsf_home);
        hg_rsf_away = view.findViewById(R.id.jl_hg_rsf_away);
        hg_dxf_big = view.findViewById(R.id.jl_hg_dxf_big);
        hg_dxf_small = view.findViewById(R.id.jl_hg_dxf_small);
        hg_sfc_ke_15 = view.findViewById(R.id.jl_hg_sfc_ke_15);
        hg_sfc_ke_610 = view.findViewById(R.id.jl_hg_sfc_ke_610);
        hg_sfc_ke_1115 = view.findViewById(R.id.jl_hg_sfc_ke_1115);
        hg_sfc_ke_1620 = view.findViewById(R.id.jl_hg_sfc_ke_1620);
        hg_sfc_ke_2125 = view.findViewById(R.id.jl_hg_sfc_ke_2125);
        hg_dxf_stop = view.findViewById(R.id.jclq_list_dxf_stop);
        hg_sfc_ke_26 = view.findViewById(R.id.jl_hg_sfc_ke_26);
        hg_sfc_zhu_15 = view.findViewById(R.id.jl_hg_sfc_zhu_15);
        hg_sfc_zhu_610 = view.findViewById(R.id.jl_hg_sfc_zhu_610);
        hg_sfc_zhu_1115 = view.findViewById(R.id.jl_hg_sfc_zhu_1115);
        hg_sfc_zhu_1620 = view.findViewById(R.id.jl_hg_sfc_zhu_1620);
        hg_sfc_zhu_2125 = view.findViewById(R.id.jl_hg_sfc_zhu_2125);
        hg_sfc_zhu_26 = view.findViewById(R.id.jl_hg_sfc_zhu_26);
        CheckBox rf = view.findViewById(R.id.jclq_child_rsf_rfsf);
        TextView compare_score = view.findViewById(R.id.jclq_hg_compare_score);
        TextView home_tag = view.findViewById(R.id.jl_dlg_hh_zhu);
        CheckBox[] boxs = {hg_sf_home, hg_sf_away, hg_rsf_home, hg_rsf_away, hg_dxf_big, hg_dxf_small,
                hg_sfc_zhu_15, hg_sfc_zhu_610, hg_sfc_zhu_1115,
                hg_sfc_zhu_1620, hg_sfc_zhu_2125, hg_sfc_zhu_26, hg_sfc_ke_15, hg_sfc_ke_610,
                hg_sfc_ke_1115, hg_sfc_ke_1620, hg_sfc_ke_2125, hg_sfc_ke_26
        };
        for (CheckBox cb : boxs) {
            cb.setOnCheckedChangeListener(null);
        }
        rf.setChecked(data.getHandicap().contains("+"));
        rf.setText(data.getHandicap());
        home_tag.setText("主队（" + data.getHomeTeam() + "）让球");

        if (data.plInfos.get(0).spValue.equals("-")) {
            view.findViewById(R.id.ll_frf).setVisibility(View.GONE);
            view.findViewById(R.id.jclq_list_stop1).setVisibility(View.VISIBLE);
        } else {
            view.findViewById(R.id.ll_frf).setVisibility(View.VISIBLE);
            view.findViewById(R.id.jclq_list_stop1).setVisibility(View.GONE);
        }
        compare_score.setText(data.getPresetScore());
        for (int i = 0; i < data.plInfos.size(); i++) {
            try {
                JCLQMatchData.SPInfo info = data.plInfos.get(i).clone();
                boxs[i].setChecked(info.isSel);
                boxs[i].setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                boxs[i].setText(info.str + "\n" + info.spValue);
                boxs[i].setTag(info);
                tempSpInfos.add(info);
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
        }
        for (CheckBox cb : boxs) {
            cb.setOnCheckedChangeListener(checklistener);
        }
        if (data.getDgStaticSaleSfcStatus() == 2) {
            jl_hg_sfc_layout.setVisibility(View.VISIBLE);
        } else {
            jl_hg_sfc_layout.setVisibility(View.GONE);
            jl_hg_sfc_stop.setVisibility(View.VISIBLE);
        }

        if (data.getStaticSaleDxfStatus() == 2) {
            hg_dxf_stop.setVisibility(View.GONE);
        }else{
            hg_dxf_stop.setVisibility(View.VISIBLE);
        }

    }

    protected String setAway() {
        try {
            return data.getAwayTeam() + "(客)";
        } catch (Exception e) {
            return "--(客)";
        }
    }

    protected String setHome() {
        try {
            return data.getHomeTeam() + "(主)";
        } catch (Exception e) {
            return "--(主)";
        }
    }

    OnCheckedChangeListener checklistener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            JCLQMatchData.SPInfo info = (JCLQMatchData.SPInfo) buttonView.getTag();
            info.isSel = isChecked;
        }
    };

    protected void onClickSure() {
        int count = 0;
        for (SPInfo spInfo : tempSpInfos) {
            int index = tempSpInfos.indexOf(spInfo);
            data.plInfos.get(index).isSel = spInfo.isSel;
            if (spInfo.isSel) {
                count++;
            }
        }
        data.clickNum = count;

        int sfc = 0;
        for (String key : sfc_arrs) {
            if (data.splInfos.get(key).isSel) {
                sfc++;
            }
        }
        data.setSfcNum(sfc);
        dismiss();
    }

    protected void onClickCancel() {
        dismiss();
    }

}
